package me.dr0n3.skyRanks.Utils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import me.dr0n3.skyRanks.Rank;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
 * @author dr0n3
 * @date 20.04.2012
 */
public class PermissionsHelper 
{
    public static HashMap<Rank, Long> hmRanks = new HashMap<>();
    public static Permission permission = null;
    
    public static void checkRanks(Player player)
    {
        MessageHelper.writeLog("Pruefe Ränge für " + player.getName(), Level.INFO);
        
        for(Rank r : hmRanks.keySet())
        {
            if(r.lTime <= IOHelper.loadPlaytime(player) && isInGroup(player, r.sOldGroup))
            {
                permission.playerAddGroup(player, r.sNewGroup);
                permission.playerRemoveGroup(player, r.sOldGroup);
                //permissions.getUser(player).addGroup(r.sNewGroup);
                //permissions.getUser(player).removeGroup(r.sOldGroup);
                player.sendMessage(ChatColor.YELLOW + "Du wurdest befördert und bist nun " + r.sNewGroup + ".");
                MessageHelper.writeLog(player.getName() + " wurde befördert.", Level.INFO);
            }
        }
        
    }
    
    public static boolean isInGroup(Player player, String s)
    {
        String[] f = permission.getPlayerGroups(player);
        //String[] f = permissions.getUser(player).getGroupsNames();
        
        for(String d : f)
        {
            if(d.equals(s))
                return true;
        }
        
        return false;
    }
    
    public static boolean isRankValid(Rank r)
    {
        if(r.sNewGroup == null || r.sOldGroup == null)
            return false;
        else
            return true;
    }
}
